package com.vint.leetcode;

import com.vint.common.TreeNode;

/*
 * 1. done
 * 2. easy
 * 3. tree, 递归
 * 判断是否为平衡树：左右子树深度相差1
 */
public class Leetcode110 {
    public boolean isBalanced(TreeNode root) { if(null == root)return true;
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        if(Math.abs(l-r)<=1&&isBalanced(root.left)&&isBalanced(root.right))
            return true;
        return false;
    }

    //深度
    public int maxDepth(TreeNode root) {
        if(null==root)return 0;
        return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
        //return root==null? 0 : Math.max(maxDepth(root.left), maxDepth(root.right))+1;
    }
    public static void main(String[] args) {
        Leetcode110 leetcode100= new Leetcode110();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(5);
        root.right = new TreeNode(4);
        root.right.right= new TreeNode(2);
        TreeNode root1 = new TreeNode(1);
        root1.left = new TreeNode(2);
        root1.left.left = new TreeNode(5);
        root1.right = new TreeNode(4);
        root1.right.right= new TreeNode(3);
    }
  }
